﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Adrr.DAL
{
	/// <summary>
	/// 数据访问类:Notices
	/// </summary>
	public partial class Notices
	{
		public Notices()
		{}
		#region  Method

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(Guid NoticeID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Notices");
			strSql.Append(" where NoticeID=@NoticeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@NoticeID", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = NoticeID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Adrr.Model.Notices model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Notices(");
			strSql.Append("NoticeID,Title,PublishTime,PublishDeptID,Contents,CreateTime,PublishUserID)");
			strSql.Append(" values (");
			strSql.Append("@NoticeID,@Title,@PublishTime,@PublishDeptID,@Contents,@CreateTime,@PublishUserID)");
			SqlParameter[] parameters = {
					new SqlParameter("@NoticeID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@Title", SqlDbType.NVarChar,100),
					new SqlParameter("@PublishTime", SqlDbType.DateTime),
					new SqlParameter("@PublishDeptID", SqlDbType.NVarChar,100),
					new SqlParameter("@Contents", SqlDbType.Text),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@PublishUserID", SqlDbType.VarChar,20)};
			parameters[0].Value = Guid.NewGuid();
			parameters[1].Value = model.Title;
			parameters[2].Value = model.PublishTime;
			parameters[3].Value = model.PublishDeptID;
			parameters[4].Value = model.Contents;
			parameters[5].Value = model.CreateTime;
			parameters[6].Value = model.PublishUserID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Adrr.Model.Notices model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Notices set ");
			strSql.Append("Title=@Title,");
			strSql.Append("PublishTime=@PublishTime,");
			strSql.Append("PublishDeptID=@PublishDeptID,");
			strSql.Append("Contents=@Contents,");
			strSql.Append("CreateTime=@CreateTime,");
			strSql.Append("PublishUserID=@PublishUserID");
			strSql.Append(" where NoticeID=@NoticeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,100),
					new SqlParameter("@PublishTime", SqlDbType.DateTime),
					new SqlParameter("@PublishDeptID", SqlDbType.NVarChar,100),
					new SqlParameter("@Contents", SqlDbType.Text),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@PublishUserID", SqlDbType.VarChar,20),
					new SqlParameter("@NoticeID", SqlDbType.UniqueIdentifier,16)};
			parameters[0].Value = model.Title;
			parameters[1].Value = model.PublishTime;
			parameters[2].Value = model.PublishDeptID;
			parameters[3].Value = model.Contents;
			parameters[4].Value = model.CreateTime;
			parameters[5].Value = model.PublishUserID;
			parameters[6].Value = model.NoticeID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(Guid NoticeID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Notices ");
			strSql.Append(" where NoticeID=@NoticeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@NoticeID", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = NoticeID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string NoticeIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Notices ");
			strSql.Append(" where NoticeID in ("+NoticeIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Adrr.Model.Notices GetModel(Guid NoticeID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 NoticeID,Title,PublishTime,PublishDeptID,Contents,CreateTime,PublishUserID from Notices ");
			strSql.Append(" where NoticeID=@NoticeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@NoticeID", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = NoticeID;

			Adrr.Model.Notices model=new Adrr.Model.Notices();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["NoticeID"]!=null && ds.Tables[0].Rows[0]["NoticeID"].ToString()!="")
				{
					model.NoticeID= new Guid(ds.Tables[0].Rows[0]["NoticeID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Title"]!=null && ds.Tables[0].Rows[0]["Title"].ToString()!="")
				{
					model.Title=ds.Tables[0].Rows[0]["Title"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PublishTime"]!=null && ds.Tables[0].Rows[0]["PublishTime"].ToString()!="")
				{
					model.PublishTime=DateTime.Parse(ds.Tables[0].Rows[0]["PublishTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["PublishDeptID"]!=null && ds.Tables[0].Rows[0]["PublishDeptID"].ToString()!="")
				{
					model.PublishDeptID=ds.Tables[0].Rows[0]["PublishDeptID"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Contents"]!=null && ds.Tables[0].Rows[0]["Contents"].ToString()!="")
				{
					model.Contents=ds.Tables[0].Rows[0]["Contents"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CreateTime"]!=null && ds.Tables[0].Rows[0]["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["PublishUserID"]!=null && ds.Tables[0].Rows[0]["PublishUserID"].ToString()!="")
				{
					model.PublishUserID=ds.Tables[0].Rows[0]["PublishUserID"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select NoticeID,Title,PublishTime,PublishDeptID,Contents,CreateTime,PublishUserID ");
			strSql.Append(" FROM Notices ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" NoticeID,Title,PublishTime,PublishDeptID,Contents,CreateTime,PublishUserID ");
			strSql.Append(" FROM Notices ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}
        /// <summary>
        /// 联合多表查询
        /// </summary>
        public DataSet GetListunion(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT NoticeID,Title,PublishTime,PublishDeptID,DeptName,Contents,Notices.CreateTime,PublishUserID,UserName ");
            strSql.Append(" FROM Notices ");
            strSql.Append("join Base_Dept on Notices.PublishDeptID=Base_Dept.DeptID left join UserInfo on PublishUserID=UserInfo.UserID");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by PublishTime desc");
            return DbHelperSQL.Query(strSql.ToString());
        }

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Notices ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.NoticeID desc");
			}
			strSql.Append(")AS Row, T.*  from Notices T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Notices";
			parameters[1].Value = "NoticeID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

